package com.fangyi.yunmusic.service.es;

import com.fangyi.yunmusic.config.ResourceConfig;
import com.fangyi.yunmusic.pojo.es.AlbumDocument;
import com.fangyi.yunmusic.pojo.es.SongDocument;
import com.fangyi.yunmusic.pojo.vo.AlbumVO;
import com.fangyi.yunmusic.pojo.vo.ArtistVO;
import com.fangyi.yunmusic.repository.es.AlbumRepository;
import com.fangyi.yunmusic.repository.es.SongRepository;
import com.fangyi.yunmusic.utils.PagedResult;
import com.fangyi.yunmusic.utils.PojoUtils;
import org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.WeightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery;

@Service
public class AlbumSearchService {

    @Autowired
    private AlbumRepository albumRepository;

    @Autowired
    private ResourceConfig resourceConfig;


    public Page<AlbumDocument> queryAlbum(Integer pageNum, Integer pageSize, String query) {

        // 设置查询条件
        FunctionScoreQueryBuilder.FilterFunctionBuilder[] functions = {
                new FunctionScoreQueryBuilder.FilterFunctionBuilder(matchPhraseQuery("name", query), new WeightBuilder().setWeight(90)), // 设置权重
                new FunctionScoreQueryBuilder.FilterFunctionBuilder(matchPhraseQuery("description", query), new WeightBuilder().setWeight(10)),
        };
        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(functions)
                .scoreMode(FiltersFunctionScoreQuery.ScoreMode.SUM)     // 设置权重分求和模式
                .setMinScore(10);   // 设置权重分最低分


        Pageable pageable = PageRequest.of(pageNum, pageSize);// 设置分页

        NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder().withPageable(pageable).withQuery(functionScoreQueryBuilder).build();

        return albumRepository.search(nativeSearchQuery);


    }


    public PagedResult queryAlbumVO(Integer pageNum, Integer pageSize, String query) {
        Page<AlbumDocument> albumDocumentPage = queryAlbum(pageNum-1, pageSize, query);
        List<AlbumDocument> albumDocuments = albumDocumentPage.getContent();
        List<AlbumVO> albumVOs = albumDocuments.stream().map(artistDocument -> {
            AlbumVO albumVO = new AlbumVO();
            PojoUtils.copyProperties(artistDocument, albumVO);
            albumVO.setPicUrl(resourceConfig.getAlbumCoverUrl(artistDocument.getPicUrlId()));
            return albumVO;
        }).collect(Collectors.toList());
        return new PagedResult(albumVOs, pageNum, albumDocumentPage.getTotalPages(), albumDocumentPage.getTotalElements());
    }


}
